package com.atguigu.auth.controller;

import com.atguigu.auth.mapper.SysUserRoleMapper;
import com.atguigu.auth.service.SysUserService;
import com.atguigu.common.Result;
import com.atguigu.common.util.MD5;
import com.atguigu.model.system.SysRole;
import com.atguigu.model.system.SysUser;
import com.atguigu.vo.system.AssginRoleVo;
import com.atguigu.vo.system.SysUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: SysUserController
 * Package: com.atguigu.auth.controller
 * Description:
 *
 * @Author 蹇泽阳
 * @Create 2023/11/14 14:28
 * @Version 1.0
 */
@RestController
@Api(tags = "用户管理接口")
@RequestMapping("/admin/system/sysUser")
public class SysUserController {
  
  @Autowired
  private SysUserService service;
  @Autowired
  private SysUserRoleMapper sysUserRoleMapper;

  
  // 用户条件分页查询
  @ApiOperation("用户条件分页查询")
  @GetMapping("{page}/{limit}")
  public Result index(@PathVariable Long page, @PathVariable Long limit,
                      SysUserQueryVo sysUserQueryVo) {
    // 创建page对象
    Page<SysUser> pageParam = new Page<>(page, limit);
    
    // 封装条件，判断条件值不为空
    LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
    // 获取条件值
    String username = sysUserQueryVo.getKeyword();
    String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
    String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();
    // 判断条件值不为空
    // like 模糊查询
    if (!StringUtils.isEmpty(username)) {
      wrapper.like(SysUser::getUsername, username);
    }
    // ge 大于等于
    if (!StringUtils.isEmpty(createTimeBegin)) {
      wrapper.ge(SysUser::getCreateTime, createTimeBegin);
    }
    // le 小于等于
    if (!StringUtils.isEmpty(createTimeEnd)) {
      wrapper.le(SysUser::getCreateTime, createTimeEnd);
    }
    
    // 调用mp的方法实现条件分页查询
    IPage<SysUser> pageModel = service.page(pageParam, wrapper);
    return Result.ok(pageModel);
  }
  
  @ApiOperation(value = "获取用户")
  @GetMapping("get/{id}")
  public Result get(@PathVariable Long id) {
    SysUser user = service.getById(id);
    return Result.ok(user);
  }
  
  @ApiOperation(value = "保存用户")
  @PostMapping("save")
  public Result save(@RequestBody SysUser user) {
    // 密码通过MD5进行加密
    user.setPassword(MD5.encrypt(user.getPassword()));
    service.save(user);
    return Result.ok();
  }
  
  @ApiOperation(value = "更新用户")
  @PutMapping("update")
  public Result updateById(@RequestBody SysUser user) {
    service.updateById(user);
    return Result.ok();
  }
  
  @ApiOperation(value = "删除用户")
  @DeleteMapping("remove/{id}")
  public Result remove(@PathVariable Long id) {
    service.removeById(id);
    return Result.ok();
  }
  
  @ApiOperation(value = "根据用户获取角色数据")
  @GetMapping("/toAssign/{userId}")
  public Result toAssign(@PathVariable Long userId) {
    List<SysRole> sysRoleList = sysUserRoleMapper.selectRoleByUserId(userId);
    // Map<String, Object> roleMap = sysRoleService.findRoleByAdminId(userId);
    return Result.ok(sysRoleList);
  }
  
  @ApiOperation(value = "根据用户分配角色")
  @PutMapping("/doAssign")
  @Transactional
  public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
    // 用户id, 角色id列表
    sysUserRoleMapper.deleteUserRoleByUserId(assginRoleVo.getUserId());
    sysUserRoleMapper.updateUserRoleByUserId(assginRoleVo.getUserId(), assginRoleVo.getRoleIdList());
    // sysRoleService.doAssign(assginRoleVo);
    return Result.ok();
  }
  
  @ApiOperation(value = "更新状态")
  @GetMapping("updateStatus/{id}/{status}")
  public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
    // sysUserService.updateStatus(id, status);
    sysUserRoleMapper.updateUserStatusById(id, status);
    
    return Result.ok();
  }
  
  
  
}
